package com.demomap;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.finalteam.galleryfinal.model.PhotoInfo;

public class InputActivity extends AppCompatActivity {

    @Bind(R.id.et_content)
    EditText etContent;
    @Bind(R.id.iv_img)
    ImageView ivImg;
    ArrayList<CardBean> list;
    CardBean bean;
    private String url, content, time;
    Date date;
    SimpleDateFormat format;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input);
        ButterKnife.bind(this);
        list = SPUtil.getInstance().loadCardBean();


    }

    @OnClick({R.id.tv_back, R.id.tv_submit, R.id.tv_img, R.id.iv_img})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_submit:
                content = etContent.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    ToastUtils.alertInputError("请输入备忘录详情", etContent);
                    break;
                }
                if (TextUtils.isEmpty(url)) {
                    ToastUtils.showToast("请设置图片");
                    break;
                }
                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                        Locale.CHINA);
                date = new Date();
                time = format.format(date);

                bean = new CardBean(url, content, time);
                list.add(bean);
                SPUtil.getInstance().saveCardBean(list);
                finish();
                break;
            case R.id.tv_img:
                ImagePickUtils.openImagePick(1, new ImagePickUtils.ImagePickListener() {
                    @Override
                    public void onHanlderSuccess(int requestCode, List<PhotoInfo> resultList) {
                        url = resultList.get(0).getPhotoPath();
                        ImageLoadManager.into(url, ivImg);
                    }

                    @Override
                    public void onHanlderFailure(int requestCode, String errorMsg) {

                    }
                });
                break;
            case R.id.iv_img:
                ImagePickUtils.openImagePick(1, new ImagePickUtils.ImagePickListener() {
                    @Override
                    public void onHanlderSuccess(int requestCode, List<PhotoInfo> resultList) {
                        url = resultList.get(0).getPhotoPath();
                        ImageLoadManager.into(url, ivImg);
                    }

                    @Override
                    public void onHanlderFailure(int requestCode, String errorMsg) {

                    }
                });
                break;
        }
    }
}
